package server.core;

import server.Container;
import server.Mapper;
import server.connector.http.HttpRequestImpl;

public class SimpleContextMapper implements Mapper {
    //关联的容器
    private SimpleContext context = null;

    //根据路径返回容器
    @Override
    public Container map(HttpRequestImpl request, boolean update) {
        String servletPath = request.getDecodedRequestURI();
        SimpleWrapper wrapper = null;
        String name = context.findServletMapping(servletPath);
        if (name != null)
            wrapper = (SimpleWrapper)context.findChild(name);
        return wrapper;
    }

    //getter---------------------------------------------------------------------
    @Override
    public Container getContainer() {
        return context;
    }
    @Override
    public String getProtocol() {
        return null;
    }

    //setter---------------------------------------------------------------------
    @Override
    public void setContainer(Container container) {
        if (!(container instanceof SimpleContext))
            throw new IllegalArgumentException("Illegal type of container");
        context = (SimpleContext)container;
    }

    @Override
    public void setProtocol(String protocol) {

    }
}
